import * as echarts from '../component/ec-canvas/echarts';
var app = getApp();
let option = app.globalData.option;
let chart;
var liunxUrl=app.globalData.liunxUrl
var localUrl=app.globalData.localUrl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
     
      onInit:function initChart(canvas, width, height) {
        chart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        canvas.setChart(chart);
      
        //这里复制了官方示例配置
        option =
        {
          title: {
            text: '个人收入支出饼状图',
            left: 'center'
          },
          tooltip: {
            trigger: 'item'
          },
          legend: {
            orient: 'vertical',
            left: 'left',
          },
          series: [
            {
              name: '说明',
              type: 'pie',
              radius: '60%',
              data: [
                { value: 0, name: '收入' },
                { value: 0, name: '支出' },
                { value: 0, name: '投资' },
                { value: 0, name: '还款' },
                { value: 0, name: '购物' },
                { value: 0, name: '房租' }
              ],
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              }
            }
          ]
        };
        console.log(option)
        option.series[0].data[0].value = app.globalData.income;
        option.series[0].data[1].value = app.globalData.expenditure;
        option.series[0].data[2].value = app.globalData.investment;
        option.series[0].data[3].value = app.globalData.other;
        option.series[0].data[4].value = app.globalData.shopping;
        option.series[0].data[5].value = app.globalData.rent;
        chart.setOption(option);
        // chart.resize({width:400,height:350})
        return chart;
      }
    },
    date: "",
    user: {}
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  dateToString(date) {
    var year = date.getFullYear();
    var month = (date.getMonth() + 1).toString();
    var day = (date.getDate()).toString();
    if (month.length == 1) {
      month = "0" + month;
    }
    if (day.length == 1) {
      day = "0" + day;
    }
    var dateTime = year + "-" + month + "-" + day;
    return dateTime;
  },
  dateToString1(date) {
    var year = date.getFullYear();
    var month = (date.getMonth() + 1).toString();
    var day = (date.getDate()).toString();
    if (month.length == 1) {
      month = "0" + month;
    }
    if (day.length == 1) {
      day = "0" + day;
    }
    var dateTime = year + "-" + month;
    return dateTime;
  },

  showSelect() {

    if(app.globalData.user==null){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      wx.navigateTo({
        url: '/pages/list/list',
      })
    }
    
  },

  getMonth(month) {

    if (this.data.user!=null) {
      wx.request({
        url: liunxUrl+'/rec/getMonth?id=' + this.data.user.id + '&month=' + month,
        method: 'get',
        success: (res) => {
          console.log(res)
          if (res.data.code == 200) {

            let dataList = res.data.data;

            let datas = [
              {
                "type": 0,
                "money": 0
              },
              {
                "type": 1,
                "money": 0
              },
              {
                "type": 2,
                "money": 0
              },
              {
                "type": 3,
                "money": 0
              },
              {
                "type": 4,
                "money": 0
              },
              {
                "type": 5,
                "money": 0
              }
            ]
            dataList.forEach(mo => {
              datas.forEach(mo1 => {
                if (mo.type == mo1.type) {
                  mo1.money = mo.money
                }
              })
            })
            if (datas.length > 0) {
              datas.forEach(element => {

                if (element.type == 0) {
                  app.globalData.income = element.money;
                }
                if (element.type == 1) {
                  app.globalData.expenditure = element.money;
                }
                if (element.type == 2) {
                  app.globalData.investment = element.money;
                }
                if (element.type == 3) {
                  app.globalData.other = element.money;
                }
                if (element.type == 4) {
                  app.globalData.shopping = element.money;
                }
                if (element.type == 5) {
                  app.globalData.rent = element.money;
                }
              });
            } else {
              app.globalData.income = 0;
              app.globalData.expenditure = 0;
              app.globalData.other = 0;
              app.globalData.investment = 0;
              app.globalData.shopping = 0;
              app.globalData.rent = 0;
            }
            console.log(option)
            option.series[0].data[0].value = app.globalData.income;
            option.series[0].data[1].value = app.globalData.expenditure;
            option.series[0].data[2].value = app.globalData.investment;
            option.series[0].data[3].value = app.globalData.other;
            option.series[0].data[4].value = app.globalData.shopping;
            option.series[0].data[5].value = app.globalData.rent;
            chart.setOption(option);
            // chart.resize({width:400,height:350})
          }
        }
      })
    }else{
      console.log(option)
      option.series[0].data[0].value = app.globalData.income;
      option.series[0].data[1].value = app.globalData.expenditure;
      option.series[0].data[2].value = app.globalData.other;
      option.series[0].data[3].value = app.globalData.investment;
      option.series[0].data[4].value = app.globalData.shopping;
      option.series[0].data[5].value = app.globalData.rent;
      chart.setOption(option);
      // chart.resize({width:400,height:350})
    }


  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
    this.data.user = app.globalData.user;
    let month = this.dateToString(new Date);
    this.setData({
      date: this.dateToString1(new Date)
    })


    this.getMonth(month);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
    this.getMonth(e.detail.value + "-01");
  },

  joke() {
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    wx.navigateTo({
      url: '/pages/joke/joke',
    })
    wx.hideLoading({

    })
  },

  billInput() {
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    if(app.globalData.user==null){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      wx.navigateTo({
        url: '/pages/billInput/billInput',
      })
    }
    wx.hideLoading({

    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})